//TODO: Proteus模拟输出到9后，会乱码
#include<reg51.h>
//最开始直接用1，2代替宏定义的两个量，发现不行，按下key1后，主机的两个数码管都会亮
#define NODE1_ADDR 1
#define NODE2_ADDR 2

unsigned char str[] = {"0123456789ABCDEF"};

unsigned char num[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xd8,0x80,0x90};

void display_2(unsigned char ch) 
{
    if((ch>=48)&&(ch<=57)) 
        P2=num[ch-47];
    else if((ch>=65)&&(ch<=70)) 
        P2=num[ch-55];
}

void display_1(unsigned char ch) 
{
    if((ch>=48)&&(ch<=57)) 
        P0=num[ch-47];
    else if((ch>=65)&&(ch<=70)) 
        P0=num[ch-55];
}


void main()
{
	SCON = 0xf0;
	TMOD = 0x20;
    TH1 = TL1 = 0xfd;
    TR1 = 1;
    ES = 1;
    EA = 1;
    while(1);
}

void delay(unsigned int time)
{
    unsigned char i, j;
    for (i = 0; i < 130; i++)
    {
        for (j = 0; j < time; j++);
    }
}

void receive() interrupt 4
{
    RI = 0;
    if (RB8 == 1)
    {
        switch(SBUF)
        {
            //必须加延时
            case NODE1_ADDR:
                SM2 = 0;
                delay(20);
                display_1(SBUF);
                break;
            case NODE2_ADDR:
                SM2 = 0;
                delay(20);
                display_2(SBUF);
                break;
        }				
        return;
    }
}
